package zhzz;

//问题所在因为还不会自定义控件所以这里是先用大量代码把id写死
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.example.animaldoctor03.IllnessDetail;
import com.example.animaldoctor03.R;
import com.example.animaldoctor03.ZhengZhuangFZ;
import com.example.animaldoctor03.ZhengZhuangFZ2;

import commonClass.FunctionDispatcher;
import commonClass.QueryAllbaseSymptomsByAnimalIDAction;
import commonClass.RemoteActionsName;
import commonClass.FunctionDispatcher.IRequestCompeletListener;
import Modle.disease.Symptoms;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class Requests extends Activity {
	private RadioGroup rdGroupof1, rdGroupof2, rdGroupof3, rdGroupof4;
	private RadioButton rdButton1 = null, rdButton2 = null;
	private Button btofrequest = null;
	private static int iofrequests = 0;
	private static int jishuofIftonext = 0 ;
	private static int jishu2 = 0;
	private CheckBox b1, b2, b3, b4, b5, b6;

	private ArrayList<Object> listofRqsr = new ArrayList<Object>();
	private String animalID = "01371054603";
	private static ArrayList<Object> resultListofrequests = null;
	private List<String> listofIds = new ArrayList<String>();
	private Symptoms idofgroup3, idofgroup4, idofgroup5;
	private Symptoms bx1 = null, bx2 = null, bx3 = null, bx4 = null,
			bx5 = null, bx6 = null;

	private void chushihua() {
		setContentView(R.layout.requests2);
		rdGroupof1 = (RadioGroup) findViewById(R.id.rdgroupofrequesr1);
		rdGroupof2 = (RadioGroup) findViewById(R.id.rdgroupofrequesr3);
		rdGroupof3 = (RadioGroup) findViewById(R.id.rdgroupofrequesr4);
		rdGroupof4 = (RadioGroup) findViewById(R.id.rdgroupofrequesr5);

		btofrequest = (Button) findViewById(R.id.btofrequests);
		rdButton1 = (RadioButton) findViewById(R.id.rdbuttonofrequest1);
		rdButton2 = (RadioButton) findViewById(R.id.rdbuttonofrequest2);

		b1 = (CheckBox) findViewById(R.id.checkbox11);
		b2 = (CheckBox) findViewById(R.id.checkbox12);
		b3 = (CheckBox) findViewById(R.id.checkbox13);
		b4 = (CheckBox) findViewById(R.id.checkbox14);
		b5 = (CheckBox) findViewById(R.id.checkbox15);
		b6 = (CheckBox) findViewById(R.id.checkbox16);
	}

	private ArrayList<Object> assembly() {

		ArrayList<Object> list = new ArrayList<Object>();

		list.add(animalID);

		return list;
	}

	private void binding(final ArrayList<Object> list) {

		try {
			for (Object s : list) {

				listofIds.add(((Symptoms) s).getSymptomsID());

			}
		} catch (NullPointerException e) {

			android.app.AlertDialog.Builder builderofRequests = new AlertDialog.Builder(
					Requests.this);
			builderofRequests.setMessage("无数据，请用其他功能");
			builderofRequests.create().show();
		}

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		chushihua();

		ArrayList<Object> parasList = assembly();
		final ArrayList<Object> resultListT = null;

		QueryAllbaseSymptomsByAnimalIDAction iAction = new QueryAllbaseSymptomsByAnimalIDAction();

		FunctionDispatcher functionDispacher = new FunctionDispatcher();

		functionDispacher.exceute(
				RemoteActionsName.QueryAllbaseSymptomsByAnimalIDAction,
				parasList, iAction, new IRequestCompeletListener() {

					@Override
					public void doCompelet(ArrayList<Object> resultList) {
						// TODO Auto-generated method stub
						Requests.this.resultListofrequests = resultList;
						binding(resultListofrequests);
					}
				} ,this);

		rdGroupof1.setOnCheckedChangeListener(new mycheckedchangeListener());
		rdGroupof2.setOnCheckedChangeListener(new mycheckedchangeListener2());
		rdGroupof3.setOnCheckedChangeListener(new mycheckedchangeListener3());
		rdGroupof4.setOnCheckedChangeListener(new mycheckedchangeListener4());

		btofrequest.setOnClickListener(new myListenerofrequest());

		b1.setOnCheckedChangeListener((android.widget.CompoundButton.OnCheckedChangeListener) new listenerofcheckB());
		b2.setOnCheckedChangeListener((android.widget.CompoundButton.OnCheckedChangeListener) new listenerofcheckB());
		b3.setOnCheckedChangeListener((android.widget.CompoundButton.OnCheckedChangeListener) new listenerofcheckB());
		b4.setOnCheckedChangeListener((android.widget.CompoundButton.OnCheckedChangeListener) new listenerofcheckB());
		b5.setOnCheckedChangeListener((android.widget.CompoundButton.OnCheckedChangeListener) new listenerofcheckB());
		b6.setOnCheckedChangeListener((android.widget.CompoundButton.OnCheckedChangeListener) new listenerofcheckB());

	}

	private class listenerofcheckB implements
			android.widget.CompoundButton.OnCheckedChangeListener {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {
			// TODO Auto-generated method stub
			if (isChecked) {
				CheckBox checkboflistener = (CheckBox) buttonView;
				String s = (String) checkboflistener.getText();
				System.out.println(s);
			}
		}

	}

	private class myListenerofrequest implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			infofcheckbox();
			if(jishu2 > 0){
				jishuofIftonext ++ ;
			}
			if(jishuofIftonext > 3){
			if (iofrequests == 1 || iofrequests == 0) {
				Intent intenttoAInessofzh = new Intent(Requests.this,
						AllInessOfZH.class);
				startActivity(intenttoAInessofzh);
			} else {
				Intent intenttoPInessofzh = new Intent(Requests.this,
						PartIllnessOfZH.class);
				startActivity(intenttoPInessofzh);
				finish();
			}
			}
			else{
				android.app.AlertDialog.Builder builderofRequests = new AlertDialog.Builder(
						Requests.this);
				builderofRequests.setMessage("每组都必须选择");
				builderofRequests.create().show();
			}
		}

	}

	private class mycheckedchangeListener implements OnCheckedChangeListener {

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			// TODO Auto-generated method stub
			if (checkedId == R.id.rdbuttonofrequest1) {
				iofrequests = 1;
			} else {
				iofrequests = 2;
			}
		}

	}

	private class mycheckedchangeListener2 implements OnCheckedChangeListener {

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			// TODO Auto-generated method stub

			if (checkedId == R.id.rdbuttonofrequest231) {
				try {
					jishuofIftonext ++ ;
					idofgroup3 = ((Symptoms) resultListofrequests.get(7));
				} catch (NullPointerException e) {

					android.app.AlertDialog.Builder builderofRequests = new AlertDialog.Builder(
							Requests.this);
					builderofRequests.setMessage("无数据，请用其他功能");
					builderofRequests.create().show();
				}
			} else if(checkedId == R.id.rdbuttonofrequest232) {
				try {
					jishuofIftonext ++;
					idofgroup3 = ((Symptoms) resultListofrequests.get(6));
				} catch (NullPointerException e) {
					android.app.AlertDialog.Builder builderofRequests = new AlertDialog.Builder(
							Requests.this);
					builderofRequests.setMessage("无数据，请用其他功能");
					builderofRequests.create().show();
				}
			}

		}

	}

	private class mycheckedchangeListener3 implements OnCheckedChangeListener {

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			// TODO Auto-generated method stub
			if (checkedId == R.id.rdbuttonofrequest241) {
				try {
					jishuofIftonext ++;
					idofgroup4 = ((Symptoms) resultListofrequests.get(8));
				} catch (NullPointerException e) {
					android.app.AlertDialog.Builder builderofRequests = new AlertDialog.Builder(
							Requests.this);
					builderofRequests.setMessage("无数据，请用其他功能");
					builderofRequests.create().show();
				}
			} else if(checkedId == R.id.rdbuttonofrequest242) {
				try {
					jishuofIftonext ++ ;
					idofgroup4 = ((Symptoms) resultListofrequests.get(9));
				} catch (NullPointerException e) {
					android.app.AlertDialog.Builder builderofRequests = new AlertDialog.Builder(
							Requests.this);
					builderofRequests.setMessage("无数据，请用其他功能");
					builderofRequests.create().show();
				}
			}

		}

	}

	private class mycheckedchangeListener4 implements OnCheckedChangeListener {

		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			// TODO Auto-generated method stub

			if (checkedId == R.id.rdbuttonofrequest251) {
				try {
					jishuofIftonext ++;
					idofgroup5 = ((Symptoms) resultListofrequests.get(10));
				} catch (NullPointerException e) {
					android.app.AlertDialog.Builder builderofRequests = new AlertDialog.Builder(
							Requests.this);
					builderofRequests.setMessage("无数据，请用其他功能");
					builderofRequests.create().show();
				}
			} else if (checkedId == R.id.rdbuttonofrequest252) {
				try {
					jishuofIftonext ++ ;
					idofgroup5 = ((Symptoms) resultListofrequests.get(11));
				} catch (NullPointerException e) {
					android.app.AlertDialog.Builder builderofRequests = new AlertDialog.Builder(
							Requests.this);
					builderofRequests.setMessage("无数据，请用其他功能");
					builderofRequests.create().show();
				}
			} else if(checkedId == R.id.rdbuttonofrequest253){
				/* idofgroup5 = ((Symptoms)resultListofrequests.get(12)); */
				try {
					jishuofIftonext ++ ;
					idofgroup5 = ((Symptoms) resultListofrequests.get(12));
				} catch (NullPointerException e) {
					android.app.AlertDialog.Builder builderofRequests = new AlertDialog.Builder(
							Requests.this);
					builderofRequests.setMessage("无数据，请用其他功能");
					builderofRequests.create().show();
				}
			}

		}

	}

	private void infofcheckbox() {

		SingleDataofzhzz singleDataofzhzz = SingleDataofzhzz.getInstance();

		if (b1.isChecked()) {
			try {
				jishu2 ++;
				bx1 = ((Symptoms) resultListofrequests.get(0));
				listofRqsr.add(bx1);
			} catch (NullPointerException e) {
				android.app.AlertDialog.Builder builderofRequests = new AlertDialog.Builder(
						Requests.this);
				builderofRequests.setMessage("无数据，请用其他功能");
				builderofRequests.create().show();
			}
			/* singleDataofzhzz.setObject(bx1); */
		}
		if (b2.isChecked()) {

			try {
				jishu2 ++;
				bx2 = ((Symptoms) resultListofrequests.get(1));
				listofRqsr.add(bx2);
			} catch (NullPointerException e) {
				android.app.AlertDialog.Builder builderofRequests = new AlertDialog.Builder(
						Requests.this);
				builderofRequests.setMessage("无数据，请用其他功能");
				builderofRequests.create().show();
			}

			/* singleDataofzhzz.setObject(bx2); */
		}
		if (b3.isChecked()) {

			try {
				jishu2 ++;
				bx3 = ((Symptoms) resultListofrequests.get(2));
				listofRqsr.add(bx3);

			} catch (NullPointerException e) {
				android.app.AlertDialog.Builder builderofRequests = new AlertDialog.Builder(
						Requests.this);
				builderofRequests.setMessage("无数据，请用其他功能");
				builderofRequests.create().show();
			}

			/* singleDataofzhzz.setObject(bx3); */
		}
		if (b4.isChecked()) {
			try {

				jishu2 ++;
				bx4 = ((Symptoms) resultListofrequests.get(3));
				listofRqsr.add(bx4);
			} catch (NullPointerException e) {
				android.app.AlertDialog.Builder builderofRequests = new AlertDialog.Builder(
						Requests.this);
				builderofRequests.setMessage("无数据，请用其他功能");
				builderofRequests.create().show();
			}

			/* singleDataofzhzz.setObject(bx4); */
		}
		if (b5.isChecked()) {
			try {
				jishu2 ++;
				bx5 = ((Symptoms) resultListofrequests.get(4));
				listofRqsr.add(bx5);
			} catch (NullPointerException e) {
				android.app.AlertDialog.Builder builderofRequests = new AlertDialog.Builder(
						Requests.this);
				builderofRequests.setMessage("无数据，请用其他功能");
				builderofRequests.create().show();
			}

			/* singleDataofzhzz.setObject(bx5); */
		}
		if (b6.isChecked()) {
			try {
				jishu2 ++;
				bx6 = ((Symptoms) resultListofrequests.get(5));
				listofRqsr.add(bx6);
			} catch (NullPointerException e) {
				android.app.AlertDialog.Builder builderofRequests = new AlertDialog.Builder(
						Requests.this);
				builderofRequests.setMessage("无数据，请用其他功能");
				builderofRequests.create().show();
			}

			/* singleDataofzhzz.setObject(bx6); */
		}
		if(idofgroup3 != null){
		listofRqsr.add(idofgroup3);}
		if(idofgroup4 != null)
		listofRqsr.add(idofgroup4);
		if(idofgroup5 != null)
		listofRqsr.add(idofgroup5); 
		/*
		 * singleDataofzhzz.setObject(idofgroup3);
		 * singleDataofzhzz.setObject(idofgroup4);
		 * singleDataofzhzz.setObject(idofgroup5);
		 */
		singleDataofzhzz.setListinformation(listofRqsr);
		List<String> listofrqst = new ArrayList<String>();
		for(int i = 0 ; i < listofRqsr.size() ; i++){
			listofrqst.add(((Symptoms)listofRqsr.get(i)).getSymptomsName());
		}
		singleDataofzhzz.setlists2ofinformation(listofrqst);
		List<String> listofrqst2 = new ArrayList<String>();
		for(int i = 0 ; i < listofRqsr.size() ; i++){
			listofrqst2.add(((Symptoms)listofRqsr.get(i)).getSymptomsID());
		}
		singleDataofzhzz.setidinformation(listofrqst2);
	}

}
